using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Data;
using System.Data.Common;
using System.ComponentModel;
using Tilver.DBConnectionUI;

namespace Tilver.DBConnectionUI.SQLite
{
    [ToolboxItem(false)]
    internal partial class SQLiteEditor : UserControl, IUDLEditor
	{
        SQLiteStringBuilder builder = new SQLiteStringBuilder();

		public SQLiteEditor()
		{
			InitializeComponent();

            txDataSource.TextChanged += delegate { builder.DataSource = txDataSource.Text; };
            txPassword.TextChanged += delegate { builder.Password = txPassword.Text; };
            txPageSize.Validated += delegate { builder.PageSize = Convert.ToInt32(txPageSize.Text); };
            txCacheSize.Validated += delegate { builder.CacheSize = Convert.ToInt32(txCacheSize.Text); };
            cboDateFormat.Validated += delegate { builder.DateTimeFormat = cboDateFormat.Text; };
            cboSync.Validated += delegate { builder.Synchronous = cboSync.Text; };
            ckUTF16.CheckedChanged += delegate { builder.UseUTF16Encoding = ckUTF16.Checked; };

            txPageSize.Validating+=new CancelEventHandler(ValidateInt32);
            txCacheSize.Validating+=new CancelEventHandler(ValidateInt32);
        }

        #region IConnectionEditor Implementation

        public void RefreshData()
        {
            txDataSource.Text = builder.DataSource;
            txPassword.Text = builder.Password;
            txPageSize.Text = Convert.ToString(builder.PageSize);
            txCacheSize.Text = Convert.ToString(builder.CacheSize);
            cboDateFormat.Text = builder.DateTimeFormat;
            cboSync.Text = builder.Synchronous;
            ckUTF16.Checked = builder.UseUTF16Encoding;

        }

        public string ConnectionString
        {
            get
            {
                return builder.ConnectionString;
            }
            set
            {
                builder.ConnectionString = value;
                RefreshData();
            }
        }

        public object GetAdvancedPropertiesObject()
        {
            return builder;
        } 

        #endregion

        #region helper methods

        private void FindDatasource(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            if (fd.ShowDialog(this.Parent) == DialogResult.OK)
            {
                builder.DataSource = fd.FileName;
                txDataSource.Text = fd.FileName;
            }
        }

        void ValidateInt32(object sender, CancelEventArgs e)
        {
            if (sender is TextBoxBase)
            {
                TextBoxBase txt = (TextBoxBase)sender;
                try
                {
                    Convert.ToInt32(txt.Text);
                    txt.ForeColor = System.Drawing.SystemColors.ControlText;
                    e.Cancel = false;
                }
                catch (Exception)
                {
                    txt.ForeColor = System.Drawing.Color.Red;
                    e.Cancel = true;
                }
            }
        } 

        #endregion
    }

}
